package com.mingda.weathermagiccenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.mingda.weathermagiccenter.mapper.WsStationDataMapper;
import com.mingda.weathermagiccenter.pojo.WsStationData;
import com.mingda.weathermagiccenter.pojo.WsStationFcData;
import com.mingda.weathermagiccenter.service.WsStationFcDataService;
import com.mingda.weathermagiccenter.mapper.WsStationFcDataMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
* @author Administrator
* @description 针对表【ws_station_fc_data(天气预报表)】的数据库操作Service实现
* @createDate 2024-12-25 09:49:30
*/
@Service
public class WsStationFcDataServiceImpl extends ServiceImpl<WsStationFcDataMapper, WsStationFcData>
    implements WsStationFcDataService{


    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveOrUpdateBatchById(List<WsStationFcData> list) {
        return SqlHelper.saveOrUpdateBatch(WsStationFcData.class, WsStationFcDataMapper.class, super.log, list, DEFAULT_BATCH_SIZE, (sqlSession, entity) -> {//这里主要是查询唯一约束对应的记录是否存在
            LambdaQueryWrapper<WsStationFcData> queryWrapper = Wrappers.<WsStationFcData>lambdaQuery()
                    .eq(WsStationFcData::getStationkey, entity.getStationkey()).eq(WsStationFcData::getTimebase,entity.getTimebase()).eq(WsStationFcData::getValidtime,entity.getValidtime());
            Map<String, Object> map = CollectionUtils.newHashMapWithExpectedSize(1);
            map.put(Constants.WRAPPER, queryWrapper);
            return CollectionUtils.isEmpty(sqlSession.selectList(getSqlStatement(SqlMethod.SELECT_LIST), map));
        }, (sqlSession, entity) -> {
            LambdaUpdateWrapper<WsStationFcData> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.eq(WsStationFcData::getStationkey, entity.getStationkey()).eq(WsStationFcData::getTimebase,entity.getTimebase()).eq(WsStationFcData::getValidtime,entity.getValidtime());
            Map<String, Object> param = CollectionUtils.newHashMapWithExpectedSize(2);
            param.put(Constants.ENTITY, entity);
            param.put(Constants.WRAPPER, lambdaUpdateWrapper);
            sqlSession.update(getSqlStatement(SqlMethod.UPDATE), param);
        });

    }



}




